package jp.sinmetal.trainer.util;

import java.util.Calendar;
import java.util.Date;

/**
 * 日付に関するUtilクラス
 * @author sin
 *
 */
public class DateUtil {
	/**
	 * 今を返す
	 * @author sin
	 *
	 * @return
	 */
	public static Date getNow() {
		Calendar calendar = Calendar.getInstance();
		return calendar.getTime();
	}

	/**
	 * 年月日を返す
	 * @author ishimura
	 *
	 * @return
	 */
	public static String getDay(Date date) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		int year = calendar.get(Calendar.YEAR);
		int month = calendar.get(Calendar.MONTH) + 1;
		int day = calendar.get(Calendar.DAY_OF_MONTH);

		return String.format("%s-%s-%s", year,month,day);
	}

	/**
	 * 時刻を返す
	 * @author ishimura
	 *
	 * @return
	 */
	public static String getTime(Date date) {
		if (date == null) {
			return null;
		}
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		int hour = calendar.get(Calendar.HOUR_OF_DAY);
		int minute = calendar.get(Calendar.MINUTE);

		return String.format("%s:%s", hour,minute);
	}


	/**
	 * 時間が過ぎているかを返す
	 * @author sin
	 *
	 * @param limitTime
	 * @return
	 */
	public static boolean isOverTime(Date limitTime) {
		return (isOverTime(limitTime, getNow()));
	}

	/**
	 * 時間が過ぎているかを返す
	 * @author sin
	 *
	 * @param limitTime
	 * @return
	 */
	public static boolean isOverTime(Date limitTime, Date comparingTime) {
		if (limitTime == null || comparingTime == null) {
			return false;
		}

		long limitTimeMills = limitTime.getTime();
		long comparingTimeMills = comparingTime.getTime();

		return (limitTimeMills <= comparingTimeMills);
	}

	/***
	 * 指定された日付から、分単位で追加した日付を返す
	 *
	 * @author ishimura
	 * @return 指定日がnullの場合は、nullを返す
	 */
	public static Date getAddMinuteTime(Date date, Integer minute) {
		if (date == null) {
			return null;
		}
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.add(Calendar.MINUTE, minute);
		return calendar.getTime();
	}

}
